package cn.sdormitory.controller.attendance;

import cn.sdormitory.attendance.entity.HolidayConfig;
import cn.sdormitory.attendance.service.HolidayConfigService;
import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;

/**
 * @author: lx
 * @date: 2023/9/22 8:50
 * @description:
 */
@Api(tags = "holiday-config => 假期配置")
@RestController
@RequestMapping("/holiday/config")
public class HolidayConfigController {

    @Autowired
    HolidayConfigService holidayConfigService;

    @PostMapping("/createHoliday")
    public CommonResult createHoliday(@RequestBody HolidayConfig holidayConfig){
//        if(DateUtils.parseDate(holidayConfig.getHolidayStart()).after(
//                DateUtils.parseDate(holidayConfig.getHolidayEnd()))){
//            return CommonResult.failed("假期开始时间大于假期结束时间，不可新增！！！");
//        }
        int num = holidayConfigService.createHoliday(holidayConfig);
        if(num > 0){
            return CommonResult.success("新增成功");
        }else {
            return CommonResult.failed("新增失败");
        }
    }

    @PostMapping("/findHolidayPageList")
    public CommonResult findHolidayPageList(@RequestBody HolidayConfig query){
        IPage<HolidayConfig> page = holidayConfigService.findHolidayPageList(query);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @GetMapping("/getHolidayById/{id}")
    public CommonResult getHolidayById(@PathVariable("id") Long id){
        HolidayConfig holiday = holidayConfigService.getHolidayById(id);
        return CommonResult.success(holiday);
    }

    @DeleteMapping("/deleteHolidayById/{id}")
    public CommonResult deleteHolidayById(@PathVariable("id") Long id){
        int num = holidayConfigService.deleteHolidayById(id);
        if(num > 0){
            return CommonResult.success("删除成功");
        }else {
            return CommonResult.failed("删除失败");
        }
    }

    @PostMapping("/updateHoliday")
    public CommonResult updateHoliday(@RequestBody HolidayConfig holiday){
        int num = holidayConfigService.updateHoliday(holiday);
        if(num > 0){
            return CommonResult.success("更新成功");
        }else {
            return CommonResult.failed("更新失败");
        }
    }
}
